package com.zhiwei.codesecurity.common.utils;

import com.zhiwei.codesecurity.common.base.DecoratorHandler;
import com.zhiwei.codesecurity.common.base.IEnum;
import com.zhiwei.codesecurity.common.base.ServiceResponse;
import com.zhiwei.codesecurity.common.enums.ResultCode;
import com.zhiwei.codesecurity.common.exception.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class ServiceResponseUtil {
    private final static Logger logger = LoggerFactory.getLogger(ServiceResponseUtil.class);


    public static <T>ServiceResponse<T> createServiceResponse(T rlt){
        return new ServiceResponse<T>(rlt);
    }

    public static <S, T>ServiceResponse<T> createServiceResponse(S rlt,DecoratorHandler<S, T> decorator){
        return new ServiceResponse<T>(decorator.invoke(rlt));
    }

    public static <T>ServiceResponse<T> createServiceResponse(){
        return new ServiceResponse<T>();
    }


    public static <S, T>ServiceResponse<List<T>> createServiceResponseForList(List<S> rlt, DecoratorHandler<S, T> decorator){
        List<T> datas = new ArrayList<T>();
        if(decorator!=null){
            if (rlt!=null&&rlt.size() > 0) {
                for (S obj : rlt) {
                    datas.add(decorator.invoke(obj));
                }
            }
        }
        return new ServiceResponse<List<T>>(datas);
    }


    public static <T>ServiceResponse<T> createServiceResponseForError(ServiceException e){
        logger.error("{}:{}",e.getMsg(),e);
        return new ServiceResponse<T>(e.getCode(),e.getMsg());
    }

    public static <T>ServiceResponse<T> createServiceResponse(IEnum resultCode,Exception e){
        logger.error("{}:{}",resultCode,e);
        return new ServiceResponse<T>(resultCode);
    }


    public static <T>ServiceResponse<T> createServiceResponse(IEnum resultCode){
        return new ServiceResponse<T>(resultCode);
    }

    public static <T>ServiceResponse<T> createServiceResponse(IEnum resultCode,String msg){
        return new ServiceResponse<T>(resultCode,msg);
    }



    public static <T>ServiceResponse<T> createServiceResponse(IEnum resultCode, String msg, Exception e){
        logger.error("{code:{},msg:{}}:{}",resultCode.getKey(),msg,e);
        return new ServiceResponse<T>(resultCode,msg);
    }

    public static <T>ServiceResponse<T> createServiceResponseForBisError(String msg,Exception e){
        return createServiceResponse(ResultCode.EXCEPTION_BUSINESS,msg,e);
    }


    public static <T>ServiceResponse<T> createServiceResponseForBisError(String msg){
        return createServiceResponse(ResultCode.EXCEPTION_BUSINESS,msg);
    }

    public static <T>ServiceResponse<T> createServiceResponseForBisError(IEnum iEnum){
        return new ServiceResponse<T>(iEnum.getKey(),iEnum.getValue());
    }

    public static <T>ServiceResponse<T> createServiceResponseForParamError(String msg){
        return createServiceResponse(ResultCode.EXCEPTION_PARAMS,msg);
    }
}
